package myjava3d;

import javax.media.j3d.Appearance;
import javax.media.j3d.BranchGroup;
import javax.media.j3d.ColoringAttributes;
import javax.media.j3d.ImageComponent2D;
import javax.media.j3d.Material;
import javax.media.j3d.Texture2D;
import javax.media.j3d.TextureAttributes;
import javax.media.j3d.Transform3D;
import javax.vecmath.Color3f;
import javax.vecmath.Color4f;

import start.Start;

import com.sun.j3d.utils.geometry.Box;
import com.sun.j3d.utils.image.TextureLoader;

public class Ground extends BranchGroup {
	
	public Ground() {
		//Farben und Oberflaeche
		Color3f col=new Color3f(0.5f,0.5f,0.5f);
	    
	    Color3f white=new Color3f(1.0f,1.0f,1.0f);	
	    Appearance ap_white = new Appearance();					// redApp - Aussehen
	    ap_white.setMaterial(new Material(white,white,white,white,100.0f));// Material 
	    ColoringAttributes ca_white=new ColoringAttributes();			// Farbattribute
	    ca_white.setShadeModel(ColoringAttributes.SHADE_GOURAUD);		// Schattierungs-Modell wird gesetzt
	    ap_white.setColoringAttributes(ca_white);	    
	    
	   //Erzeugung des Objekts: Die Lok
	    Appearance gras = this.texture("gras.jpg",256,256);
	    gras.setMaterial(new Material(white,white,white,col,150.0f));
		
		//Boden
		Box boden = new Box(0.9f, 0.05f, 0.5f, Box.GENERATE_TEXTURE_COORDS + Box.GENERATE_NORMALS, gras);
		
		addChild(boden);
	}
	
	
	public Appearance texture(String s, int breite, int hoehe) {
		TextureLoader loader = new TextureLoader(s, Start.getMyObject());
        ImageComponent2D image = loader.getImage();
        Texture2D texture = new Texture2D();
        int form  = texture.getFormat();
        int base = texture.getMipMapMode();
        texture = new Texture2D(base,form,breite,hoehe);
        texture.setImage(0, image);
        Appearance appear = new Appearance();
        appear.setTexture(texture);
        TextureAttributes texat = new TextureAttributes( 
        		TextureAttributes.COMBINE_INTERPOLATE,new Transform3D(),
        		new Color4f(),
        		TextureAttributes.NICEST );
        appear.setTextureAttributes(texat);
        appear.setMaterial(new Material());
        return(appear);
	}
}
